﻿using System;
using System.Globalization;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace SW.Lite.Core.Extensions
{
    /// <summary>
    /// Class HtmlExtension.
    /// </summary>
    public static class HtmlExtension
    {
        /// <summary>
        /// Pagers the specified HTML.
        /// </summary>
        /// <param name="html">The HTML.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="recordCount">The record count.</param>
        /// <returns>MvcHtmlString.</returns>
        public static MvcHtmlString Pager(this HtmlHelper html, int pageSize, int recordCount)
        {
            var pageCount = (recordCount - 1) / pageSize + 1;
            var rvs = html.ViewContext.RouteData.Values;
            var outPutSb = new StringBuilder();
            int pageIndex;
            Int32.TryParse(rvs["page"].ToString(), out pageIndex);
            for (var i = 0; i < 10; i++)
            {
                if ((pageIndex + i - 5) < 1 || (pageIndex + i - 5) > pageCount) continue;

                if (i == 5)
                    outPutSb.AppendFormat("<a class=\"paginate_active\">{0}</a>", pageIndex);
                else
                {
                    rvs["page"] = pageIndex + i - 5;
                    outPutSb.Append(html.RouteLink((pageIndex + i - 5).ToString(CultureInfo.InvariantCulture), rvs));
                }
            }

            return new MvcHtmlString(outPutSb.ToString());
        }
    }
}
